package semantic.tables;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import syntaxtree.Type;


public class MethodTable {

	private final List<Type> formalTypes = new ArrayList<Type>();
	private final Map<Symbol, Type> vars = new HashMap<Symbol, Type>();
	public final Symbol id;
	public final Type returnType;

	public MethodTable(String id, Type returnType) {
		this.id = Symbol.symbol(id);
		this.returnType = returnType;
	}

	public boolean addFormal(String key, Type value) {
		formalTypes.add(value);
		return addVar(key, value);
	}

	public boolean addVar(String key, Type value) {
		if (vars.containsKey(Symbol.symbol(key))) {
			return false;
		}
		vars.put(Symbol.symbol(key), value);
		return true;
	}

	public Type getVarType(String key) {
		return vars.get(Symbol.symbol(key));
	}

	public boolean formalTypeListEquals(List<Type> formalTypeList) {
		if (formalTypes.size() != formalTypeList.size()) {
			return false;
		}
		for (int i = 0; i < formalTypes.size(); i++) {
			if (!formalTypes.get(i).getClass().equals(formalTypeList.get(i).getClass())) {
				return false;
			}
		}
		return true;
	}
}
